package com.aiit.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.aiit.shiro.realms.CustomerRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: LuTao
 * @Date: 2022/04/28/14:36
 */
/*用来整合shiro框架相关的配置类*/
@Configuration
public class ShiroConfig {

    //1.创建shiroFilter  负责拦截所有请求
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManage){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //给filter设置一个安全管理器
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManage);
        //配置系统受限资源
        //配置系统公共资源
        Map<String,String> map=new HashMap<>();
//        map.put("/user/login","anon");//anon设置为公共资源
//        map.put("/user/register","anon");
//        map.put("/login","anon");
//        map.put("/register","anon");
//        map.put("/user/getImage","anon");
//        map.put("/**","authc");//authc 请求这个资源需要认证和授权 相当于filter

        //shiroFilterFactoryBean有一个默认认证界面路径
//        shiroFilterFactoryBean.setLoginUrl("login.html");
        //通过map配置系统受限资源和配置系统公共资源
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }

    //2.构建安全管理器
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(Realm realm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        //给安全管理器设置
        defaultWebSecurityManager.setRealm(realm);
        return  defaultWebSecurityManager;
    }

    //获取从数据库查出的角色和权限
    @Bean(name = "shiroDialect")
    public ShiroDialect Dialect(){
        return new ShiroDialect();
    }

    //3.自定义realm
    @Bean
    public Realm getRealm(){
        CustomerRealm customerRealm = new CustomerRealm();
        //修改凭证校验匹配器
        HashedCredentialsMatcher credentialsMatcher=new HashedCredentialsMatcher();
        //设置加密算法MD5
        credentialsMatcher.setHashAlgorithmName("MD5");
        //设置散列次数
        credentialsMatcher.setHashIterations(1024);
        customerRealm.setCredentialsMatcher(credentialsMatcher);
        //开启缓存管理
        customerRealm.setCacheManager(new EhCacheManager());
        customerRealm.setCachingEnabled(true);//开启全局缓存
        customerRealm.setAuthenticationCachingEnabled(true);//认证认证缓存
        customerRealm.setAuthenticationCacheName("authenticationCache");//改写认证缓存name
        customerRealm.setAuthorizationCachingEnabled(true);//开启授权缓存
        customerRealm.setAuthorizationCacheName("authorizationCache");
        return customerRealm;
    }

}
